package com.wangtao.firstapp.fragments.homepagerfragmnets;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.RVSifeAdapter;
import com.wangtao.firstapp.modles.SifeData;
import com.wangtao.firstapp.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 设计感页面
 * Created by lanouhn on 16/8/24.
 */
public class DesignFragment extends Fragment{

    private XRecyclerView designRecyclerView;//创建recyclerView
    private List<SifeData.DataBean.TopicBean> designSifeData=new ArrayList<>();//创建实体类对象  !!!复用一个实体类
    private List<SifeData.DataBean.TopicBean> datas=new ArrayList<>();//中间值

    int page=0;

    private RVSifeAdapter designRVSifeAdapter;//创建RVSifeAdapter适配器对象，！！！复用的


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_sift_listview,container,false);
        designRecyclerView= (XRecyclerView) view.findViewById(R.id.recyclerView_sift_listView);

        //上拉加载的动画效果
        designRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //recyclerView.setLaodingMoreProgressStyle(ProgressStyle.Pacman);
        //下拉刷新的动画效果
        designRecyclerView.setArrowImageView(R.drawable.ic_loading_rotate);

        designRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                page = 0;
                getDataFromWeb();
            }

            //上拉加载
            @Override
            public void onLoadMore() {
                page++;
                getDataFromWeb();
            }
        });

        getDataFromWeb();
        return view;
    }

    private void getDataFromWeb() {
        try {
             String url="http://open4.bantangapp.com/topics/topic/listByScene";
             String params="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2560255&oauth_token=4df22230db195bba652ef0e593881122&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page="+page+"&pagesize=20&id=16";

            HttpUtils.doPostAsyn(url,params, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_design=new Gson();
                    datas=gson_design.fromJson(result,SifeData.class).getData().getTopic();
                    if (datas.size()>0){
                        handler.sendEmptyMessage(1);
                    }else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 0:
                break;
            case 1:

                if (page==0){//
                    //
                    designSifeData.clear();
                    //
                    designSifeData.addAll(datas);

                    designRVSifeAdapter = new RVSifeAdapter(getActivity(), designSifeData);
                    designRecyclerView.setAdapter(designRVSifeAdapter);
                    designRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                    //
                    designRecyclerView.refreshComplete();
                    datas.clear();
                }else {
                    designSifeData.addAll(datas);
                    designRVSifeAdapter.notifyDataSetChanged();
                    designRecyclerView.loadMoreComplete();
                    datas.clear();
                }

//                designRVSifeAdapter=new RVSifeAdapter(getActivity(),designSifeData);
//                designRecyclerView.setAdapter(designRVSifeAdapter);
//                designRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                break;
        }
    }
};

}
